﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ComputationVisualizer;
using DissipativeInputProcessing;

namespace ComputationVisualizer
{
    /// <summary>
    /// Interaction logic for CommonPaletteControl.xaml
    /// </summary>
    public partial class CommonPaletteControl : UserControl
    {
        private CommonPaletteCreator _paletteCreator;

        public CommonPaletteControl()
        {
            InitializeComponent();
            _paletteCreator = new CommonPaletteCreator(paletteMinValTB, paletteMaxValTB, paletteMinBtn, paletteMaxBtn);
            _paletteCreator.PaletteUpdated += new RoutedEventHandler(_paletteCreator_PaletteUpdated);
        }

        #region Public Properties

        public IFramePool FramePool { get; set; }

        public IFrame CurrentFrame { get; set; }

        public FramePalette Palette { get { return _paletteCreator.GenerateCurrentPalette(); } }

        #endregion

        #region Events
        private event EventHandler _paletteUpdated;
        public event EventHandler PaletteUpdated
        {
            add { _paletteUpdated += value; }
            remove { _paletteUpdated -= value; }
        }
        #endregion

        #region Private Methods

        private void RaiseEvent(EventHandler handler)
        {
            if (handler != null)
                handler(this, null);
        }

        #endregion

        #region UI Updates

        private void commonPaletteCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                FramePool.CommonPalette = _paletteCreator.GenerateCurrentPalette();
            }
            catch (FormatException)
            {
                MessageBox.Show("Введены некорректные значения.");
            }
            catch (ArgumentException)
            {
                MessageBox.Show("Некорректные цвета");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Непредвиденная ошибка:\n" + ex.Message + "\nПожалуйста, сообщите об этой ошибке");
            }
            RaiseEvent(_paletteUpdated);
        }

        private void commonPaletteCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            FramePool.CommonPalette = null;
            RaiseEvent(_paletteUpdated);
        }

        private void createCommonPaletteBtn_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Эта операция может зянять пару минут. Продолжить?","Внимание!", MessageBoxButton.YesNo,MessageBoxImage.Warning, MessageBoxResult.No) == MessageBoxResult.Yes)
            {
                _paletteCreator.SetPalette(FramePool.CreateCommonPalette());
                RaiseEvent(_paletteUpdated);
            }
        }

        private void createFramePaletteBtn_Click(object sender, RoutedEventArgs e)
        {
            if (CurrentFrame != null)
                _paletteCreator.SetPalette(FramePool.CreatePaletteAccordingToFrame(CurrentFrame));
        }

        private void _paletteCreator_PaletteUpdated(object sender, RoutedEventArgs e)
        {
            var palette = _paletteCreator.GenerateCurrentPalette();
            if (palette != null)
                FramePool.CommonPalette = palette;
            RaiseEvent(_paletteUpdated);
        }

        #endregion
    }
}
